<template>
  <div class="RankingList">
    <van-nav-bar
      title="排行榜"
      left-text="返回"
      right-text="按钮"
      style="background-color: #E4463B; color:#fff;"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="music" size="18" />
      </template>
    </van-nav-bar>
    <div class="listContent">
      <div class="title">云音乐官方榜</div>
      <canvas
        id="canvas"
        width="115"
        height="120"
        style="display:none"
      ></canvas>
      <div class="officialList" v-for="(item, index) in list" :key="index">
        <div
          v-if="comparison(item.name, 'officialList')"
          @click="updatePopup(item)"
        >
          <van-card
            width="115"
            height="120"
            :tag="item.updateFrequency"
            :thumb="item.coverImgUrl"
            style="position: relative;"
          >
            <template #desc>
              <van-cell
                v-for="(element, i) in item.tracks"
                :key="i"
                :title="`${i + 1}. ${element.first}- ${element.second}`"
              />
            </template>
          </van-card>
        </div>
      </div>
      <div class="otherTitle">全球榜</div>
      <van-row>
        <div v-for="item in list" :key="item.id">
          <van-col
            span="8"
            v-if="comparison(item.name, 'otherTitle')"
            @click="updatePopup(item)"
          >
            <van-image width="116" height="112" :src="item.coverImgUrl">
              <template>
                <div class="musicUpdatemsg">{{ item.updateFrequency }}</div>
              </template>
            </van-image>
            <div class="imgTitle">{{ item.name }}</div>
          </van-col>
        </div>
      </van-row>
      <div class="otherTitle">曲风榜</div>
      <van-row>
        <div v-for="item in list" :key="item.id">
          <van-col span="8" v-if="comparison(item.name, 'featureMusic')"  @click="updatePopup(item)">
            <van-image width="116" height="112" :src="item.coverImgUrl">
              <template>
                <div class="musicUpdatemsg">{{ item.updateFrequency }}</div>
              </template>
            </van-image>
            <div class="imgTitle">{{ item.name }}</div>
          </van-col>
        </div>
      </van-row>
    </div>
    <ranking-list
      ref="ranking"
      :MusicListContent="MusicListContent"
      :MusicListShow.sync="MusicListShow"
    ></ranking-list>
  </div>
</template>

<script>
import { getSonSheet } from '@/api/RankingList.js'
import RankingList from './componet/RankingList.vue'
export default {
  components: { RankingList },
  data () {
    return {
      list: [], // 排行页面所有的数据
      // 官方榜的列表
      officialList: ['飙升榜', '新歌榜', '原创榜', '热歌榜'],
      otherList: [
        '美国Billboard榜',
        'UK排行榜周榜',
        'iTunes榜',
        '日本Oricon榜',
        '法国 NRJ Vos Hits 周榜',
        '云音乐欧美新歌榜',
        '云音乐欧美热歌榜',
        '云音乐日语榜',
        '云音乐韩语榜',
        '俄语榜',
        '越南语榜',
        '黑胶VIP爱听榜'
      ],
      featureMusic: [
        '云音乐电音榜',
        '云音乐ACG榜',
        '云音乐说唱榜',
        '云音乐摇滚榜',
        '云音乐民谣榜',
        '云音乐国电榜',
        '云音乐古典榜',
        '云音乐古风榜'
      ],
      MusicListShow: false,
      MusicListContent: { bgc: [255, 255, 255, 255] }
    }
  },
  created () {
    this.getSonSheet()
  },
  mounted () {},
  methods: {
    onClickLeft () {
      this.$router.back()
    },
    onClickRight () {},
    async getSonSheet () {
      const { artistToplist, list } = await getSonSheet()
      this.list = [...list, { ...artistToplist }]
      this.list.forEach(element => console.log(element.name))
    },
    comparison (name, list) {
      if (list === 'officialList') {
        return this.officialList.some(item => item === name)
      } else if (list === 'otherTitle') {
        return this.otherList.some(item => item === name)
      } else if (list === 'featureMusic') {
        return this.featureMusic.some(item => item === name)
      }
    },
    updatePopup (content) {
      const img = new Image()
      const canvas = document.getElementById('canvas')
      const ctx = canvas.getContext('2d')
      img.src = content.coverImgUrl
      img.crossOrigin = 'anonymous'
      /* ctx.drawImage(img, 0, 0)
      const imgData = ctx.getImageData(0, 0, img.width, img.height).data
      return [imgData[0], imgData[1], imgData[2], imgData[3]] */
      const that = this
      img.onload = function () {
        ctx.drawImage(img, 0, 0)
        const imgData = ctx.getImageData(0, 0, img.width, img.height).data
        that.MusicListContent = {
          ...content,
          bgc: [imgData[0], imgData[1], imgData[2], imgData[3]]
        }
        that.MusicListShow = true
      }
    }
  }
}
</script>

<style scoped lang="less">
.RankingList {
  width: 750px;
  height: 3414px;
  /deep/.van-nav-bar__title {
    color: #fff;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff !important;
  }

  /deep/.van-nav-bar__text {
    color: #fff !important;
  }
  /deep/.van-icon {
    color: #fff;
  }
  .title {
    width: 194px;
    height: 46px;
    line-height: 46px;
    padding: 24px 0;
    font-size: 32px;
    text-align: center;
    font-family: PingFangSC-regular;
  }
  /deep/.listContent {
    padding: 0 20px;
    .van-cell {
      height: 80px;
      line-height: 80px;
      padding: 0;
    }
    .van-card__thumb {
      width: 230px;
      height: 240px;
    }
    .van-card {
      background-color: #fff;
      padding: 5px 0;
      &:first-child {
        padding-top: 0;
      }
      &:last-child {
        padding-top: 0;
      }
      .van-cell__title span {
        font-size: 14px;
      }
    }
  }
  .van-card__tag {
    top: 190px;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
  }
  /deep/.van-card__tag .van-tag--danger {
    background-color: rgba(0, 0, 0, 0);
  }
  .otherTitle {
    line-height: 160px;
    height: 160px;
  }
  .van-col .van-image {
    display: block;
    padding: 0 4px;
    position: relative;
    /deep/img {
      display: block;
      border-radius: 10px;
    }
  }
  /deep/.musicUpdatemsg {
    position: absolute;
    color: #fff;
    bottom: 10px;
    left: 20px;
    font-size: 24px;
  }
  .van-row .imgTitle {
    font-size: 16px;
    // overflow: hidden;
    max-width: 220px;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: center;
    margin: 0 auto;
    line-height: 44px;
    padding-bottom: 10px;
  }
}
</style>
